<?php
/*
 * sxHttpClient
 *
 */

class sxHttpClient
{
	public $host='';
	public $port=0;
	public $errno=0;
	public $errstr='';
	public $retCode=0;
	public $retDescr='';
	public $aHeader=array();
	public $content='';
	private $fh=null;
	private $URL='';

	public function connect($URL, $proxy='') {
		$this->URL = $URL;
		$this->errno = 0;
		$this->errstr = '';
		$this->retCode = 0;
		$this->retDescr = '';
		$this->content = '';
		$this->aHeader = array();
		$this->fh = null;
		if (strtolower(substr($URL,0,7)) != 'http://') {
			$this->errno = 801;
			$this->error = "wrong URL - must begin with http://<br>$URL";
			return false;
		}
	   list($host) = explode('/',substr($URL,7));
	   $aHost = explode(':',$host);
	   $this->host = $aHost[0];
	   if (isset($aHost[1])) {
	   	$this->port = $aHost[1];
	   } else {
	   	$this->port = 80;
	   }
	   if (empty($proxy)) {
	   	$proxy_host = $this->host;
	   	$proxy_port = $this->port;
	   } else {
		   $aProxy = explode(':',$proxy);
		   $proxy_host = $aProxy[0];
		   if (!isset($aProxy[1])) {
		      $proxy_port = '80';
		   } else {
		   	$proxy_port = $aProxy[1];
		   }
	   }		
	   $errno="";
	   $errstr="";
	   $this->fh = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 5);
	   $this->errno = $errno;
	   $this->error = $errstr.' (fsockopen)';
	   if(!$this->fh) { 
	   	$this->fh = null;
	   	return false;
	   }
	   return true;
	}
	
	public function close() {
		if ($this->fh) {
			fclose($this->fh);
		   $this->fh = null;
		}
	}
	
	public function GET($URL, $proxy='') {
		if ($this->fh == null) {
			if (!$this->connect($URL, $proxy='')) { 
				return false;
			}
		}
		$out  = "GET $URL HTTP/1.1\r\n";
		$out .= "Host: ".$this->host."\r\n";
		$out .= "Connection: Close\r\n\r\n";
	   fputs($this->fh,$out);
	   $risp = '';
	   $rec = fgets($this->fh, 4096);
	   $this->retCode = substr($rec,9,3);
	   $this->retDescr = trim(substr($rec,13));
	   while ($rec) {
	     $risp .= $rec;
	     $rec = fread($this->fh, 4096);
	   }
	   $this->close();
	   $this->aHeader = explode("\n",trim(substr($risp,0,strpos($risp,"\r\n\r\n"))));
	   $this->content = substr($risp,strpos($risp,"\r\n\r\n")+4);
	   if ($this->retCode != '200') {
	      return false;
	   }
		return true;
	}
	
	public function POST($URL, $sPost, $proxy='') {
		if ($this->fh == null) {
			if (!$this->connect($URL, $proxy='')) { 
				return false;
			}
		}
		$out  = "POST $URL HTTP/1.1\r\n";
		$out .= "Host: ".$this->host."\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Content-length: " . strlen($sPost) . "\r\n";
		$out .= "Connection: Close\r\n\r\n";
	   fputs($this->fh,$out.$sPost);
	   $risp = '';
	   $rec = fgets($this->fh, 4096);
	   $this->retCode = substr($rec,9,3);
	   $this->retDescr = trim(substr($rec,13));
	   while ($rec) {
	     $risp .= $rec;
	     $rec = fread($this->fh, 4096);
	   }
	   $this->close();
	   $this->aHeader = explode("\n",trim(substr($risp,0,strpos($risp,"\r\n\r\n"))));
	   $this->content = substr($risp,strpos($risp,"\r\n\r\n")+4);
	   if ($this->retCode != '200') {
	      return false;
	   }
		return true;
	}
}
